package com.imooc.repository;

import com.imooc.domain.Employee;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.RepositoryDefinition;
import org.springframework.data.repository.query.Param;

import java.util.List;


/**
 * Created by zghgchao 2017/12/29 17:09
 */
@RepositoryDefinition(domainClass = Employee.class, idClass = Integer.class)
public interface EmployeeRepository {//extends Repository<Employee, Integer>

    Employee findByName(String name);

    //where name like ?% and age < ?
    List<Employee> findByNameStartingWithAndAgeLessThan(String name, Integer age);

    //where name like %? and age < ?
    List<Employee> findByNameEndingWithAndAgeLessThan(String name, Integer age);

    //where name in (?,?...) or age < ?
    List<Employee> findByNameInOrAgeLessThan(List<String> names, Integer age);

    //where name in (?,?...) and age < ?
    List<Employee> findByNameInAndAgeLessThan(List<String> names, Integer age);

    @Query("select o from Employee o where id=(select max(id) from Employee t1)")
    Employee getEmployeeByMaxId();

    @Query("select o from Employee o where o.name = ?1 and o.age = ?2")
    List<Employee> queryParam1(String name, Integer age);

    @Query("select o from Employee o where o.name = :name and o.age = :age")
    List<Employee> queryParam2(@Param(value = "name") String name, @Param(value = "age") Integer age);

    @Query("select o from Employee o where o.name like %?1%")
    List<Employee> queryLike1(String name);

    @Query("select o from Employee o where o.name like %:name%")
    List<Employee> queryLike2(@Param(value = "name") String name);

    @Query(nativeQuery = true, value = "select COUNT(1) from Employee;")
    long getCount();

    @Modifying
    @Query("update Employee o set o.age=:age where o.id=:id")
    void updata(@Param("id") Integer id, @Param("age") Integer age);
}
